package com.framework.base.listener;

import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.framework.base.constant.WebConstants;
import com.framework.base.util.BeanUtil;
import com.framework.base.util.CommonActionPower;
import com.framework.base.util.LoggerUtil;
import com.framework.base.util.MemCacheUtil;
import com.framework.base.util.SysConfUtil;
import com.framework.main.sys.module.domain.ModuleVO;
import com.framework.main.sys.module.service.ModuleService;
import com.framework.main.sys.module.service.ModuleServiceImp;

/**
 * Description:此监听器用于对系统进行初始化 tabaleName(对应的表名): Company: framework
 * 
 * @author：HYH
 * @version 1.0
 * @Date 2011-6-20 上午11:31:44
 */
@SuppressWarnings("serial")
public class InitializationListener extends HttpServlet implements ServletContextListener {
	private Timer timer = null;
	Logger logger=Logger.getLogger(MemCacheUtil.class);
	public void contextDestroyed(ServletContextEvent arg0) {
		if (timer != null) {
			timer.cancel();
		}
	}

	public void contextInitialized(ServletContextEvent arg0) {
		// 1.初始化获取bean对象的applictioncontext
		ServletContext sc = arg0.getServletContext();
		BeanUtil.ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		// iframe的前缀
		sc.setAttribute(WebConstants.IFRAMEPREFIX,SysConfUtil.getConfValue("iframePrefix"));
		// 设置建设单位
		sc.setAttribute("buildunit", SysConfUtil.getBuildUnit());
		
		try {
			sc.setAttribute(WebConstants.COMMONACTION,CommonActionPower.getAction());
		} catch (Exception e1) {
			LoggerUtil.logError(e1, logger, "");
			e1.printStackTrace();
		}
		 
		//用于修改数据库中应用的名称
		ModuleService moduleService=(ModuleService) BeanUtil.getServiceBeanById("moduleService");
		ModuleVO moduleVo=new ModuleVO();
		moduleVo.setModuleIdf("-1");
		moduleVo.setNamef(SysConfUtil.getBuildUnit());
		moduleVo.setParentIdf("-2");
		try {
			moduleService.doUpdate(moduleVo);
			ModuleServiceImp.isUpdateCache=true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		
	}

}
